package com.maotai.app.timetask;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.maotai.app.service.MaotaiAppService;
import com.maotai.app.thread.MaoTaiAppInfoCheckThread;
import com.maotai.llzhou.util.IPFactory;

public class MaotaiAppInfoCheckTask {
	@Resource(name = "maotaiAppService")
	private MaotaiAppService maotaiAppService;

	public void beginTask() {
		try {
			List<Map<String, Object>> list = maotaiAppService.getMaotaiAppInfoCheck();
			if (list != null && list.size() > 0) {
				JSONArray jArray = IPFactory.getIpAndPort();
				int count = list.size();
				int countData = jArray.size();
				if (countData < count) {
					count = countData;
				}
				
				for (int i = 0; i < count; i++) {
					Map<String, Object> zhanghao = list.get(i);
					JSONObject jj = (JSONObject) jArray.get(i);
					String ipAndPort = jj.getString("ip")+":"+jj.getString("port");
//					ipAndPort = IPFactory.getIpByQuanWang().trim();
					MaoTaiAppInfoCheckThread tt = new MaoTaiAppInfoCheckThread(maotaiAppService,zhanghao,ipAndPort);
					tt.start();
					Thread.sleep(1000);
					zhanghao.put("checkstatus", "1");
					maotaiAppService.updateMaotaiAppInfoCheckStatus(zhanghao);
				}
				
				//TODO
				//预约两个产品，需要修改2个地方
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
